/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.Component;
import java.awt.Dialog;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import olympicgameslogic.Athlete;

/**
 *
 * @author Nuno
 */
public class HtmlExporter {

   public static String showSaveDialog(Component c) {

      JFileChooser fc = new JFileChooser();
      FileFilter filter = new FileFilter() {

	 String description = "", extension = "";

	 @Override
	 public String getDescription() {
	    description = "HTML File";
	    extension = "html";
	    return description;
	 }

	 @Override
	 public boolean accept(File f) {
	    if (f == null) {
	       return false;
	    }
	    if (f.isDirectory()) {
	       return true;
	    }
	    return f.getName().toLowerCase().endsWith(extension);
	 }
      };

      fc.addChoosableFileFilter(filter);
      if (fc.showSaveDialog(c) == JFileChooser.APPROVE_OPTION) {
	 String caminho = fc.getSelectedFile().getPath();
	 if (!caminho.substring(caminho.length() - 4).equalsIgnoreCase("html")) {
	    caminho = caminho + ".html";
	 }
	 return caminho;
      } else {
	 return "";
      }
   }

   public static void gravarHTML(Component c, String caminho, String html) {
      try {
	 FileWriter fstream = new FileWriter(caminho, false);
	 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(caminho), "ISO-8859-1"));
	 out.write(html);
	 out.newLine();
	 out.close();
	 fstream.close();


	  JOptionPane.showMessageDialog(c, "File saved", "Success", JOptionPane.INFORMATION_MESSAGE);

      } catch (Exception ex) {
	 JOptionPane.showMessageDialog(c, "Error: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
	 return;
      }
   }
   
   
   public static String getHtmlRanking(Object[][] data, String title){
      
      
      StringBuilder sb = new StringBuilder();
        sb.append("<html>\n");
	 sb.append("<head>\n");
	 sb.append("</head>\n");

	 sb.append("<body>\n");

	 sb.append("<p><b><font face=\"Tahoma\" size=\"5\">").append(title).append("</font></b></p>\n");


	 sb.append("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"46%\" id=\"AutoNumber1\" height=\"20\">\n");
	 sb.append("<tr>\n");
	 
	 sb.append("<td width=\"10%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Position</font></b></td>\n");
	 
	 sb.append("<td width=\"15%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Country</font></b></td>\n");

	 sb.append("<td width=\"25%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Name</font></b></td>\n");
	 
	 sb.append("<td width=\"10%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Gold Medals</font></b></td>\n");
	
	 
	 sb.append("<td width=\"10%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Silver Medals</font></b></td>\n");
	 
	 
	 sb.append("<td width=\"10%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Bronze Medals</font></b></td>\n");
	 
	 
	 sb.append("<td width=\"10%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 sb.append("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Total</font></b></td>\n");
	 
	 
	 for (int i = 0; i < data.length; i++) {
	    sb.append("<tr>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][0]).append("</font></td>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append((data[i][2])).append("</font></td>\n");
	    sb.append("<td width=\"35%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][3]).append("</font></td>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][4]).append("</font></td>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][5]).append("</font></td>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][6]).append("</font></td>\n");
	    sb.append("<td width=\"10%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;").append(data[i][7]).append("</font></td>\n");
	    sb.append("</tr>\n");
	 }
	 
	 sb.append("</tr>\n");
	 

	 sb.append("</table>\n");

	 sb.append("</body>\n");

	 sb.append("</html>\n");
	 
	 
	 return sb.toString();
	 
	 
      
      
   }
}
